﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace XFGameFramework
{

    public static class ScriptableObjectExtensions
    {
#if UNITY_EDITOR

        // Fix编码

        /// <summary>
        /// 在当前选中的路径下创建资源文件
        /// </summary>
        /// <param name="scriptableObject"></param>
        [Obsolete("已过时,请示使用特性:[CreateAssetMenu()]代替!")]
        public static void CreateAsset(this ScriptableObject scriptableObject)
        {
            string path = AssetDatabase.GetAssetPath(Selection.activeObject);

            if (!AssetDatabase.IsValidFolder(path))
                path = System.IO.Path.GetDirectoryName(path);

            string name = GetName(scriptableObject.GetType().Name, path);

            AssetDatabase.CreateAsset(scriptableObject, string.Format("{0}/{1}.asset", path, name));
            Selection.activeObject = scriptableObject;
        }

        private static string GetName(string name, string path)
        {
            string tempName = name;


            string[] matchs = AssetDatabase.FindAssets(string.Format("t:ScriptableObject {0}", tempName), new string[] { path });

            int count = 0;

            while (matchs != null && matchs.Length > 0)
            {
                count++;


                tempName = string.Format("{0} ({1})", name, count);
                matchs = AssetDatabase.FindAssets(string.Format("t:ScriptableObject {0}", tempName), new string[] { path });
            }


            return tempName;
        }

#endif
    }

}

